package fr.univ.lille1.server.daoimpl;

import java.util.List;

import javax.ejb.Stateless;

import fr.univ.lille1.server.dao.MessageDao;
import fr.univ.lille1.server.domain.Message;

@Stateless
public class MessageDaoImpl extends DaoImpl<Integer, Message> implements MessageDao
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3001245992482388654L;

	public List<Message> findByLocation(float gpsLat, float gpsLong, float range)
	{
		float constant = 0.01f;
		Float minLat = gpsLat - constant;
        Float maxLat = gpsLat + constant;
        Float minLong = gpsLong - constant;
        Float maxLong = gpsLong + constant;
		return list("SELECT m FROM Message m"
				+ " WHERE m.latitude >= ?1"
				+ " AND m.latitude <= ?2"
				+ " AND m.longitude >= ?3" 
				+ " AND m.longitude <= ?4", minLat, maxLat, minLong, maxLong);
	}
	
	@Override
	public List<Message> findByUser(int userId) {
		return list("SELECT m FROM Message m"
				+ "WHERE m.user = ?1", userId);
	}
}
